package com.imooc.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/weixin")
@Slf4j
public class WeixinController {

    // 获取 code
    // https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx87e22c9342801806&redirect_uri=http://3gqpbe.natappfree.cc/sell/weixin/auth&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect

    // 获取 access_token
    //  https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx87e22c9342801806&secret=e7b6b017e1a3a707bb81340b06de9f64&code=033IOF0006cx5M16If400EQYSM0IOF0J&grant_type=authorization_code
    @GetMapping("/auth")
    public void auth(@RequestParam("code") String code){
        log.info("进入了auth方法");
        log.info("code={}",code);

        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx87e22c9342801806&secret=e7b6b017e1a3a707bb81340b06de9f64&code="+code+"&grant_type=authorization_code";
        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.getForObject(url, String.class);
        log.info("response={}",response);

        // open_id "openid":"oQstg6AKCvYdIbuePqim1zU69HvI"
        //response={"access_token":"47_SLIuDp6DkxmyVOvvDuV8V-2s4OZLiS3qPMOcocDjPiX_oDeOPNucSb9Qscea36NaWG71K38ng1FSNUIH-He4qg","expires_in":7200,"refresh_token":"47_R1fWlqLxRdworpscTOF0RgQm9fYz_snI6Qukcj24xyXOexO-kQiIyxGYgRejMN9UaCJo9mljhJnJQuwg54Omyg","openid":"oQstg6AKCvYdIbuePqim1zU69HvI","scope":"snsapi_base"}
    }

}
